package com.lxy.cxcyxmmsbackend.sys.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lxy.cxcyxmmsbackend.sys.entity.*;
import com.lxy.cxcyxmmsbackend.sys.exceptionHandle.exce.NormalException;
import com.lxy.cxcyxmmsbackend.sys.req.PageReq;
import com.lxy.cxcyxmmsbackend.sys.res.Result;
import com.lxy.cxcyxmmsbackend.sys.service.*;
import com.lxy.cxcyxmmsbackend.sys.utils.UUIDUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 刘新颖
 * @since 2024-03-02
 */
@RestController
@RequestMapping("/xmkjSq")
public class XmkjSqController {

    @Autowired
    XmkjSqService xmkjSqService;

    @Resource
    QxglUserService qxglUserService;

    @Resource
    CxcyXmService cxcyXmService;

    @Autowired
    XmfjService xmfjService;

    @ApiOperation("查询项目空间申请列表")
    @PostMapping("/queryXmKjSq")
    @Transactional
    public Result queryXmKjSq(@RequestBody PageReq pageReq){
        Integer limitS = pageReq.getCurrentPage();
        Integer pageS = pageReq.getPageSize();
        Page<XmkjSq> pages = new Page<>(limitS, pageS);
        QueryWrapper<XmkjSq> wrapper = new QueryWrapper<>();
        String xmmc = pageReq.getKeyWord().get("xmmc");
        String sqsj = pageReq.getKeyWord().get("sqsj")  ;
        String sxwcqk = pageReq.getKeyWord().get("sxwcqk");
        String status = pageReq.getKeyWord().get("status");
        //项目名称
        if (xmmc!=null && !"".equals(xmmc)){
            wrapper.lambda().eq(XmkjSq::getXmMc,xmmc);
        }
        //申请时间
        if (sqsj!=null && !"".equals(sqsj)){
            wrapper.lambda().eq(XmkjSq::getSqsj,sqsj);
        }

        //申请状态
        if (status!=null && !"".equals(status)){
            wrapper.lambda().eq(XmkjSq::getStatus,status);
        }

        //手续完成状态
        if (sxwcqk!=null && !"".equals(sxwcqk)){
            wrapper.lambda().eq(XmkjSq::getSxwcqk,sxwcqk);
        }

        wrapper.lambda().orderByDesc(XmkjSq::getSqsj);

        IPage<XmkjSq> page = xmkjSqService.page(pages, wrapper);

        HashMap<Object, Object> map = new HashMap<>();

        map.put("pageNo",limitS);
        map.put("pageSize",pageS);
        List<XmkjSq> records = page.getRecords();

        map.put("rows",records);
        map.put("total",page.getTotal());
        map.put("pageNums",(int)Math.ceil((double) page.getTotal()/pageS));

        return Result.success("查询项目空间申请记录成功",map);
    }

    @ApiOperation("申请项目空间")
    @PostMapping("/saveXmkjsq")
    @Transactional
    public Result addXmkjsq(@RequestBody XmkjSq xmkjSq, HttpServletRequest request) {
        String userBh = request.getHeader("userBh");
        QxglUser user = qxglUserService.getById(userBh);
        String id = xmkjSq.getId();
        if (id == null || "".equals(id)){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

            xmkjSq.setSqsj(sdf.format(new Date(System.currentTimeMillis())));

            xmkjSq.setId(UUIDUtil.getUUID8("", 8));
            xmkjSq.setSqrbh(userBh);
            xmkjSq.setSqrmc(user.getUserMc());
            xmkjSq.setStatus("0");
            xmkjSq.setSxwcqk("0");
            boolean save = xmkjSqService.save(xmkjSq);
            if (!save) {
                throw new NormalException("新增项目空间申请失败");
            }
        }else {
            boolean save = xmkjSqService.updateById(xmkjSq);
            if (!save) {
                throw new NormalException("修改项目空间申请失败");
            }
        }
        return Result.success("保存项目空间申请成功", xmkjSq);
    }

    @ApiOperation("查询项目撤销申请详情")
    @PostMapping("/getXmKjsqDetailByXmbh/{xmBh}")
    public Result getXmKjsqDetail(@PathVariable("xmBh") String xmBh) {
        QueryWrapper<XmkjSq> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(XmkjSq::getXmBh,xmBh);
        XmkjSq xmkjSq = xmkjSqService.getOne(queryWrapper);
        CxcyXm cxcyXm = cxcyXmService.getById(xmBh);
        xmkjSq.setXmfjHm(cxcyXm.getXmKjhm());
        if (xmkjSq == null) {
            throw new NormalException("查询项目撤销申请详情失败");
        }
        return Result.success("查询项目撤销申请详情成功", xmkjSq);
    }

    @ApiOperation("查询项目撤销申请详情")
    @PostMapping("/getXmKjsqDetailById/{id}")
    public Result getXmKjsqDetailById(@PathVariable("id") String id) {
        XmkjSq xmkjSq = xmkjSqService.getById(id);
        CxcyXm cxcyXm = cxcyXmService.getById(xmkjSq.getXmBh());
        xmkjSq.setXmfjHm(cxcyXm.getXmKjhm());
        if (xmkjSq == null) {
            throw new NormalException("查询项目撤销申请详情失败");
        }
        return Result.success("查询项目撤销申请详情成功", xmkjSq);
    }

    @ApiOperation("审批项目空间申请")
    @PostMapping("/approvalXmKjsq")
    @Transactional
    public Result approvalXmKjsq(@RequestBody XmkjSq xmkjSq, HttpServletRequest request) {
        String userBh = request.getHeader("userBh");
        QxglUser user = qxglUserService.getById(userBh);
        xmkjSq.setSprbh(userBh);
        xmkjSq.setSprmc(user.getUserMc());
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        xmkjSq.setSpsj(format.format(new Date(System.currentTimeMillis())));
        boolean save = xmkjSqService.updateById(xmkjSq);

        String xmBh = xmkjSq.getXmBh();
        CxcyXm cxcyXm = cxcyXmService.getById(xmBh);
        cxcyXm.setXmKjhm(xmkjSq.getXmfjHm());
        QueryWrapper<Xmkj> wrapper = new QueryWrapper<>();
        wrapper.lambda().like(Xmkj::getXmfjHm,xmkjSq.getXmfjHm());
        Xmkj one = xmfjService.getOne(wrapper);

        cxcyXm.setXmKjhm(xmkjSq.getXmfjHm());
        cxcyXm.setXmKj(one.getXmfjBh());
        boolean b = cxcyXmService.updateById(cxcyXm);
        if (!save || !b) {
            throw new NormalException("审批项目空间申请失败");
        }
        return Result.success("审批项目空间申请成功", xmkjSq);
    }

    @ApiOperation("提交项目空间申请")
    @PostMapping("/tijiaoXmKjsq/{id}")
    @Transactional
    public Result tijiaoXmfjDsjl(@PathVariable("id") String id){
        XmkjSq byId = xmkjSqService.getById(id);
        byId.setStatus("1");
        boolean save = xmkjSqService.updateById(byId);
        if (!save){
            throw new NormalException("提交项目空间申请失败");
        }
        return Result.success("提交项目空间申请成功",byId);
    }

    @ApiOperation("删除项目空间申请")
    @PostMapping("/removeXmKjsq/{xmBh}")
    @Transactional
    public Result removeXmKjsq(@PathVariable("xmBh") String xmBh) {
        boolean remove = xmkjSqService.removeById(xmBh);
        if (!remove) {
            throw new NormalException("删除项目空间申请失败");
        }
        return Result.success("删除项目空间申请成功", null);
    }

}

